<?php
class Sx_db_mod extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * 用户注册检查用户名
     */
    function checkuser($name) {
        $data = TRUE;
        $this -> db -> select('user_login');
        $this -> db -> where('user_login', $name);
        $this -> db -> or_where('user_email', $name);
        $this -> db -> from('sx_users');
        $Q = $this -> db -> get();
        if ($Q -> num_rows() > 0) {
            $data = FALSE;
        }
        $Q -> free_result();
        return $data;
    }

    /*
     * 用户注册
     */
    function sx_reg($myarr) {
        $myRet = array();
        if ($this -> db -> insert('sx_users', $myarr)) {
            @session_start();
            $_SESSION['hck_user'] = $myarr['user_nicename'];
            $myRet['User_mobi_key'] = $myarr['user_mobi_key'];
            $myRet['User_session_id'] = @session_id();
            $myRet['Op_id'] = @mysql_insert_id();
            return ($this -> fengchaoclass -> showhckmsg("0", $myRet, $this -> lang -> line('hck_reg_Y'), TRUE));
        }

    }

    /*
     * 用户登录
     * $u 用户名
     * $p 密码
     */

    function sx_login($u, $p) {
        $this -> db -> where("user_login", "$u");
        $this -> db -> or_where('user_email', "$u");
        $this -> db -> from('sx_users');
        $Q = $this -> db -> get();
        if ($Q -> num_rows() > 0) {
            $row = $Q -> row_array();
            if ($this -> passwordhash -> CheckPassword($p, $row['user_pass'])) {
                $user_nicename = $row['user_nicename'];
                $newkey = $this -> fengchaoencode -> url_base64_encode($u . "||" . $user_nicename . "||" . time() . "||sxhck");
                //$this -> db -> where("user_login", "$u");
                @session_start();
                $_SESSION['hck_user'] = $u;
                $myRet['User_mobi_key'] = $newkey;
                $myRet['User_session_id'] = @session_id();
                $myRet['Op_id'] = $row['ID'];
                return ($this -> fengchaoclass -> showhckmsg("0", $myRet, $this -> lang -> line('hck_login_Y'), TRUE));
            } else {
                return ($this -> fengchaoclass -> showhckmsg("", "", $this -> lang -> line('hck_login_PN'), TRUE));
            }

        } else {
            /*用户名不存在*/
            return ($this -> fengchaoclass -> showhckmsg("", "", $this -> lang -> line('hck_login_UN'), TRUE));
        }
        $Q -> free_result();
    }

    /*
     * 重置密码检查邮件是否存在
     */
    function checkemail_resetpwd($n) {
        $data = FALSE;
        $this -> db -> select('user_email');
        $this -> db -> where('user_email', "$n");
        $this -> db -> from('sx_users');
        $Q = $this -> db -> get();
        if ($Q -> num_rows() > 0) {
            $data = TRUE;
        } else {
            $data = FALSE;
        }
        $Q -> free_result();
        return $data;
    }

    /**
     *  绑定外部账户
     */
    function set_bind($id, $b) {
        $arr = array("uid" => "$id", "bind_sys" => "$b");

        if ($this -> db -> insert('sx_bind', $arr)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     *  返回用户详细信息
     */
    function get_user($uid) {

        $this -> db -> select("ID,user_email,user_login,user_nicename,user_city,user_img");
        $this -> db -> where("ID", $uid);
        $Q = $this -> db -> get("sx_users");
        if ($Q -> num_rows() > 0) {
            $row = $Q -> row_array();
            $row['CountFriend'] = $this -> get_CountFirend($uid);
            $row['CountArticle'] = $this -> get_CountArticle($uid);            
            $row['CountComment'] = $this -> get_Comment($uid);
            $row['Article'] = $this -> get_Article($uid, NULL, NULL);
            return $row ;
        } else {
            return FALSE;
        }
        $Q -> free_result();

    }

    /**
     * 获取用户好友数量
     */
    function get_CountFirend($id = NULL) {
        if (empty($id)) {
            return 0;

        } else {
            $this -> db -> where("friend_uid", $id);
            $Y = $this -> db -> get("sx_friend");
            if ($Y -> num_rows() > 0) {
                return $Y -> num_rows();
            } else {
                return 0;
            }
            $Y -> free_result();
        }

    }

    /**
     * 获取发布文章
     */
    function get_Article($id, $cursor = "", $offset = "") {
        if (empty($id)) {
            return 0;
        } else {
            $this -> load -> model("sx_article_mod");
            empty($cursor) ? $cursor = 0 : $cursor;
            empty($offset) ? $offset = 6 : $offset;
            $Narr = array();
            $this -> db -> where("user_id", $id);
            $this -> db -> limit($offset, $cursor);
            $N = $this -> db -> get("sx_article");
            if ($N -> num_rows() > 0) {
                foreach ($N->result_array() as $row) {
                    if (empty($row['pic_thumb'])) {
                        $filename = substr($row['pic_path'], strrpos($row['pic_path'], "/") + 1);
                        $pic_thumb = substr($filename, 0, -4) . "_thumb.jpg";
                        $row['pic_thumb'] = base_url() . "upload/thumb/" . $pic_thumb;
                    }
                    $row['comment']=$this->sx_article_mod->getcomment($row['pic_id'], "", "");
                    $Narr[] = $row;
                }
                return $Narr;
            } else {
                return 0;
            }
            $N -> free_result();
        }
    }

    /**
     * 获取用户评论数
     */
    function get_Comment($id) {
        if (empty($id)) {
            return 0;
        } else {
            $this -> db -> select("author");
            $this -> db -> where("author", $id);
            $M = $this -> db -> get("sx_comment");
            if ($M -> num_rows() > 0) {
                return $M -> num_rows();
            } else {
                return 0;
            }
            $M -> free_result();
        }
    }

    /**
     * 统计用户发帖个数
     */
    function get_CountArticle($id = "") {
        if (empty($id)) {
            return 0;
        } else {
            $this -> db -> select("user_id");
            $this -> db -> where("user_id", $id);
            $P = $this -> db -> get("sx_article");
            if ($P -> num_rows() > 0) {
                return $P -> num_rows();
            } else {
                return 0;
            }
            $P -> free_result();
        }
    }

}
